<?php

namespace app\modules\Admin\models;

use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;
use memberModel;

class memberpickgoodslogModel extends Model
{
    protected $table = 'member_pickgoods_log';
    protected $primaryKey = 'id';
    protected $hidden = [];
    protected $appends = ['event_text', 'oper_type_text'];

    protected static array $events = [
        'getcard'=>'领取提货卡',
        'pickgoods'=>'提货卡提货',
        'transfer'=>'转让提货卡',
        'refund'=>'提货卡退货',
        'recover'=>'回收提货卡',
    ];

    protected static array $operTypes = [
        'admin'=>'管理员',
        'system'=>'系统',
        'user'=>'用户',
    ];
    /**
     * @param DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * @return string
     */
    public function getEventTextAttribute()
    {
        $event_text = '';
        $event_key = array_keys(self::$events);
        if(in_array($this->attributes['event'], $event_key)){
            $event_text = self::$events[$this->attributes['event']];
        }
        unset($event_key);
        return $event_text;
    }

    /**
     * @return string
     */
    public function getOperTypeTextAttribute()
    {
        $oper_type_text = '';
        $oper_type_key = array_keys(self::$operTypes);
        if(in_array($this->attributes['oper_type'], $oper_type_key)){
            $oper_type_text = self::$operTypes[$this->attributes['oper_type']];
        }
        unset($oper_type_key);
        return $oper_type_text;
    }

    public function member_pickgoods_card()
    {
        return $this->hasOne(memberpickgoodscardModel::class, 'id', 'member_pickgoods_card_id');
    }

    public function pickgoods_card()
    {
        $pickgoods_card_table = (new pickgoodscardModel())->getTable();
        return $this->hasOneThrough(pickgoodscardModel::class, memberpickgoodscardModel::class,
            'id',
            'id',
            'member_pickgoods_card_id',
            'card_id'
        )->select(["{$pickgoods_card_table}.id", "{$pickgoods_card_table}.name", "{$pickgoods_card_table}.amount"]);
    }

    public function member()
    {
        $member_table = (new memberModel())->getTable();
        return $this->hasOneThrough(memberModel::class, memberpickgoodscardModel::class,
            'id',
            'id',
            'member_pickgoods_card_id',
            'member_id'
        )->select(["{$member_table}.id", "{$member_table}.phone", "{$member_table}.nickname", "{$member_table}.realname", "{$member_table}.avatar"]);
    }

}